package com.csxy.jqr.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csxy.jqr.entity.EduSubject;
import com.csxy.jqr.entity.excel.SubjectData;
import com.csxy.jqr.exception.MyException;
import com.csxy.jqr.service.EduSubjectService;

public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
    // 因为SubjectExcelListener不能交给Spring管理，需要自己new，不能注入其他对象
    // 不能实现数据库操作
    private EduSubjectService eduSubjectService;

    // 有参构造，传递eduSubjectService用于操作数据库
    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }
    // 无参构造
    public SubjectExcelListener() {

    }

    // 读取excel内容，一行一行读取
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        //判断excel中是否有内容，若无内容则抛出自定义异常
        //表示excel中没有数据，就不需要读取了
        if (subjectData == null) {
            throw new MyException(20001, "excel为空，无数据，读取异常！！！");
//            throw new MyException(20001, "添加失败");
        }

        // 一行一行读取，每次读取有两个值，第一个值一级分类，第二个值二级分类
        // 判断是否有一级分类是否重复
        EduSubject existOneSubject = this.existOneSubject(eduSubjectService,subjectData.getOneSubjectName());
        if (existOneSubject == null) { // 没有相同一级分类，进行添加
            existOneSubject = new EduSubject();
            existOneSubject.setParentId("0");//设置一级分类id值，0代表一级分类
            existOneSubject.setTitle(subjectData.getOneSubjectName());//设置一级分类名
            eduSubjectService.save(existOneSubject);//给数据库添加一级分类
        }

        //获取一级分类id值
        String parent_id = existOneSubject.getId();
        //判断是否有二级分类是否重复
        EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService,subjectData.getTwoSubjectName(),parent_id);
        if (existTwoSubject == null){ //没有相同的二级分类，进行添加
            existTwoSubject = new EduSubject();
            existTwoSubject.setParentId(parent_id); //设置二级分类父id值
            existTwoSubject.setTitle(subjectData.getTwoSubjectName());//设置二级分类名称
            eduSubjectService.save(existTwoSubject);//给数据库添加二级分类
        }
    }

    // 判断一级分类不能重复添加
    private EduSubject existOneSubject(EduSubjectService eduSubjectService, String name) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",0);
        EduSubject oneSubject = eduSubjectService.getOne(wrapper);
        return oneSubject;
    }
    // 判断二级分类不能重复添加
    private EduSubject existTwoSubject(EduSubjectService eduSubjectService, String name, String parentId) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",parentId);
        EduSubject twoSubject = eduSubjectService.getOne(wrapper);
        return twoSubject;
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
